import axios from "axios";
import store from "./store";
import router from "./router";

// 设置axios拦截器,设置token
export default function setAxiosFilter() {
  axios.interceptors.request.use(
    config => {
      if (store.getters.token) {
        config.headers.Authorization = store.getters.token;
      }
      return config;
    },
    error => {
      return Promise.reject(error);
    }
  );

  //每次请求后，都经过这个拦截器
  axios.interceptors.response.use(
    response=> {
        console.log(response);
        if(response.data.code === 200){
            const data = response.data;
            if(data.code != '000000') {
                store.commit('setToken', '');
                localStorage.removeItem('token');
                //跳转login页面
                router.replace('/login');
            }

            //登录成功



            return data;
        }
        return response;
    }
  )
}